﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using XTeam.Document.Entities;
using XTeam.Document.Entities.Words;

namespace XTeam.Documents.DataAccess
{
    public class DataBase : DbContext
    {
        public DbSet<ArticleEntity> ArticleTable { get; set; }

        public DbSet<ArticleClassifyEntity> ArticleClassifyTable { get; set; }

        public DbSet<CommentEntity> CommentTable { get; set; }

        public DbSet<WordsEntity> WordsTable { get; set; }
        public DbSet<WordsDescEntity> WordsDescTable { get; set; }
        public DbSet<WordsPhraseEntity> WordsPhraseTable { get; set; }

        public DataBase(string connectionString)
            : base(connectionString)
        {

        }

        public DataBase()
            : base("xTeamDataConnection")
        {

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            #region 文章数据表

            var articleTable = modelBuilder.Entity<ArticleEntity>();
            articleTable.HasKey(i => i.ArticleId);
            articleTable.ToTable("Article");

            #endregion

            #region 分类列表

            var articleClassifyTable = modelBuilder.Entity<ArticleClassifyEntity>();
            articleClassifyTable.HasKey(i => i.ClassId);
            articleClassifyTable.Ignore(i => i.Parent);
            articleClassifyTable.Ignore(i => i.Children);
            articleClassifyTable.ToTable("Classify");

            #endregion

            #region 评论列表
            var commentTable = modelBuilder.Entity<CommentEntity>();
            commentTable.HasKey(i => i.CommentId);
            commentTable.ToTable("Comment");
            #endregion

            #region words

            // 单词主表
            var wordsTable = modelBuilder.Entity<WordsEntity>();
            wordsTable.HasKey(i => i.Word);

            wordsTable.ToTable("Words");

            // 单词描述
            var wordsDescTable = modelBuilder.Entity<WordsDescEntity>();
            wordsDescTable.HasKey(i => i.Id);
            wordsDescTable.ToTable("Words_Description");

            // 短语
            var wordsPhraseTable = modelBuilder.Entity<WordsPhraseEntity>();
            wordsPhraseTable.HasKey(i => i.Id);
            wordsPhraseTable.Property(i => i.ChineseDesc).HasColumnName("chinese");
            wordsPhraseTable.ToTable("Words_Phrase");


            #endregion


            base.OnModelCreating(modelBuilder);
        }


    }
}
